package axter.testrecyclerview;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by zhaobo on 2015/12/24.
 */
public class MainActivity extends Activity {

	@Bind(R.id.recyclerView)
	RecyclerView recyclerView;

	@Bind(R.id.btn_del)
	Button btn_del;

	@Bind(R.id.btn_grid)
	Button btn_grid;

	List<String> dataset = new ArrayList<>(100);
	LinearLayoutManager layoutManager;

	private String toStrings(Object... objs) {
		StringBuffer sb = new StringBuffer();
		for (Object obj : objs) {
			sb.append(obj.toString());
			sb.append("|");
		}
		return sb.toString();
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		ButterKnife.bind(this);

		// 创建一个线性布局管理器
//		layoutManager = new LinearLayoutManager(this);
		setRecyclerViewManagerType(2);
		// 设置布局管理器
		recyclerView.setLayoutManager(layoutManager);
		// 设置动画效果
		recyclerView.setItemAnimator(new DefaultItemAnimator());
		recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
				super.onScrollStateChanged(recyclerView, newState);
			}

			@Override
			public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
				Log.i("onScrolled", toStrings(recyclerView.getChildAt(0).getTop(), dx, dy));
				super.onScrolled(recyclerView, dx, dy);
			}
		});
		// 创建数据集
		for (int i = 0; i < 100; i++) {
			dataset.add("item" + i);
		}
		// 创建Adapter，并指定数据集
		final MyAdapter adapter = new MyAdapter(dataset);
		// 设置Adapter
		recyclerView.setAdapter(adapter);


		btn_del.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				int pos = dataset.size() - 6;
				dataset.remove(pos);
				adapter.notifyItemRemoved(pos);
			}
		});

		btn_grid.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				if ((int) recyclerView.getTag() == 1)
					setRecyclerViewManagerType(2);
				else if ((int) recyclerView.getTag() == 2) {
					setRecyclerViewManagerType(1);
				}
			}
		});
	}

	protected void setRecyclerViewManagerType(int type) {
		int scrollPosition = 0;
		if (recyclerView.getLayoutManager() != null) {
			scrollPosition = ((LinearLayoutManager) recyclerView
					.getLayoutManager())
					.findFirstCompletelyVisibleItemPosition();
		}
		switch (type) {
			//网状
			case 1:
				layoutManager = new GridLayoutManager(this, 3);
				break;
			//线性，如list
			case 2:
				layoutManager = new LinearLayoutManager(this);
				layoutManager.canScrollHorizontally();
				break;
			default:
				layoutManager = new LinearLayoutManager(this);
				break;
		}
		recyclerView.setTag(type);
		recyclerView.setLayoutManager(layoutManager);
		recyclerView.scrollToPosition(scrollPosition);
	}
}
